package com.yazi.mxz.common.db.dao.impl;


import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Subqueries;
import org.springframework.stereotype.Repository;

import com.yazi.mxz.common.db.dao.SpecTypeDao;
import com.yazi.mxz.common.entity.beans.Category;
import com.yazi.mxz.common.entity.beans.Goods;
import com.yazi.mxz.common.entity.beans.SpecType;
import com.yazi.mxz.common.entity.beans.StoreGoods;
import com.myframework.smhj.hibernate.dao.BasicDao;

/**
 * Dao实现：商品规格类型
 *
 * @author mo_yq5
 * @since 2016年6月1日
 */
@Repository("specTypeDao")
public class SpecTypeDaoImpl extends BasicDao<SpecType> implements SpecTypeDao {

	public SpecTypeDaoImpl() {
		super(SpecType.class);
	}

	@Override
	protected Criteria getExample(SpecType specType, String searchWord) {
		Criteria c = super.getExample(specType, searchWord);
		
		// 关联对象条件
		if (null != specType) {
			// 系统分类
			Category cat = specType.getCategory();
			if (null != cat) {
				// 分类ID
				if (null != cat.getCatId()) {
					DetachedCriteria dc = DetachedCriteria.forClass(Category.class);
					dc.add(Restrictions.sqlRestriction("1=1 START WITH cat_id=" + cat.getCatId() + " CONNECT BY PRIOR cat_id= parent_id "));
					dc.setProjection(Projections.property("catId"));
					c.add(Subqueries.propertyIn("category.catId", dc));
				} else {
					// ...分类其它条件
				}
			}
			
			// 店铺商品
			StoreGoods sg = specType.getStoreGoods();
			if (null != sg) {
				// 店铺商品ID
				if (null != sg.getStoreGoodsId()) {
					c.add(Restrictions.eq("storeGoods.storeGoodsId", sg.getStoreGoodsId()));
				} else {
					// ...店铺商品其它条件
				}			
			}
			
			// 商品库
			Goods goods = specType.getGoods();
			if (null != goods) {
				// 商品库ID
				if (null != goods.getGoodsId()) {
					c.add(Restrictions.eq("goods.goodsId", goods.getGoodsId()));
				} else {
					// ...商品库其它条件
				}
			}
			
			// ...其它关联对象条件
		}
		
		return c;
	}
}
